package com.lcz.googleplay.fragment;

import android.view.View;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lcz.googleplay.adapter.AppAdapter;
import com.lcz.googleplay.config.GameProtocol;
import com.lcz.googleplay.domain.AppInfo;
import com.lcz.googleplay.utils.GsonUtils;
import com.lcz.googleplay.utils.UIUtils;
import com.lcz.googleplay.utils.ViewUtils;
import com.lcz.googleplay.widget.LoadingFrameLayout;
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener;

import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by hp on 2016/10/25.
 */

public class GameFragment extends BaseFragment {
    private List<AppInfo> listBeen;

    @Override
    public View createSuccessView() {
        ListView listView = new ListView(UIUtils.getActivity());
        listView.setAdapter(new AppAdapter(listBeen,listView) {
            @Override
            protected List<AppInfo> onload() {
                GameProtocol gameProtocol = new GameProtocol();
                String json = gameProtocol.load(listBeen.size());
                Type type = new TypeToken<List<AppInfo>>() {
                }.getType();
                Gson gson = new Gson();
                List<AppInfo> newAppInfos = gson.fromJson(json, type);
                listBeen.addAll(newAppInfos);
                return newAppInfos;
            }
        });
        // 第二个参数 慢慢滑动的时候是否加载图片 false  加载   true 不加载
        //  第三个参数  飞速滑动的时候是否加载图片  true 不加载
        listView.setOnScrollListener(new PauseOnScrollListener(ViewUtils.getImageLoader(), false, true));
        return listView;
    }

    @Override
    public LoadingFrameLayout.LoadResult load() {
        GameProtocol gameProtocol = new GameProtocol();
        String json = gameProtocol.load(0);
//        Type type = new TypeToken<List<AppInfo>>() {
//        }.getType();
//        Gson gson = new Gson();
//        listBeen = gson.fromJson(json, type);
        listBeen = GsonUtils.jsonToList(json, AppInfo[].class);
        return checkData(listBeen);
    }
}
